package bjut.core.Message;

import bjut.core.enums.RpcResponseEnum;
import bjut.core.interfaces.Serializer;
import lombok.Data;

import java.io.Serializable;

/**
 * @Author: zhuzhiming
 * @Date: 2022/4/8 22:32
 */
@Data
public class RpcResponse<T> implements Serializable {


    private static final long serialVersionUID = 212044874545664210L;

    private String requestId;

    private Integer code;

    private String message;

    private T data;

    public static <T> RpcResponse<T> success(T data, String requestId) {

        RpcResponse<T> rpcResponse = new RpcResponse<>();
        rpcResponse.setCode(RpcResponseEnum.SUCCESS.getCode());
        rpcResponse.setMessage(RpcResponseEnum.SUCCESS.getMessage());
        rpcResponse.setRequestId(requestId);
        if (data != null) {
            rpcResponse.setData(data);
        }
        return rpcResponse;
    }

    public static <T> RpcResponse<T> fail(RpcResponseEnum rpcResponseEnum) {
        RpcResponse<T> rpcResponse = new RpcResponse<>();
        rpcResponse.setCode(rpcResponseEnum.getCode());
        rpcResponse.setMessage(rpcResponseEnum.getMessage());
        return rpcResponse;
    }


}
